<?php
class View {
    protected $template_dir;
    protected $template;
    public function __construct($template_dir, $template) {
        $this->template_dir = $template_dir;
        $this->template = $template;
    }
    public static function getInstance($template_dir, $template) {
        if (preg_match('/(html|php)$/i', $template, $group)) {
            if ($group[0] == 'html') {
                return new SmartyView($template_dir, $template); 
            } else {
                return new PHPView($template_dir, $template);
            }
        } else if (file_exists($template_dir.$template.'.php')) {
            return new PHPView($template_dir, $template.'.php');
        } else if (file_exists($template_dir.$template.'.html')) {
            return new SmartyView($template_dir, $template.'.html');
        } else {
            throw new Exception("Error template name : $template");
        }
    }
    public function rander($model) {}
}
class SmartyView extends View {
    public function rander($model) {
        $smarty = new Smarty();
        $smarty->caching = false;
		$smarty->cache_dir = $this->template_dir."/../tmp";
		$smarty->php_handling = false;
		$smarty->security = true;
		$smarty->secure_dir = Array($this->template_dir);
		$smarty->config_dir   = $this->template_dir;
		$smarty->template_dir = $this->template_dir;
		$smarty->compile_dir = $this->template_dir."/../tmp";
		$smarty->compile_check = true;
		$smarty->assign($model);
		
		echo $smarty->fetch($this->template_dir.$this->template);
    }
}
class PHPView extends View {
    public function rander($model) {
        foreach($model as $name => $value){
	      $$name = $value;    
	    }
//	    $template_dir = $this->template_dir;
	    require_once($this->template_dir.$this->template);
    }
}
?>
